package com.sdehualu.utils;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;

import java.io.*;

public class S3Utils {

    // public static final Logger log = LoggerFactory.getLogger(S3Utils.class);

    private String endpoint;
    // 公钥

    private String accessKey;
    // 密钥

    private String secretKey;
    // 桶

    private String bucketName;
    // 协议类型
    private String hps;
    // 是否开启分片上传
    // private boolean multipartUpload;

    private static AmazonS3 s3Client;
    private static Protocol protocol;


    public S3Utils(String endpoint, String accessKey, String secretKey, String bucketName, String hps) {
        this.endpoint = endpoint;
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucketName = bucketName;
        this.hps = hps;
        if (hps.equals("https")) {

            protocol = Protocol.HTTPS;
        } else {

            protocol = Protocol.HTTP;
        }
        s3Client = getClient();
    }


    public AmazonS3 getClient() {
        if (null == s3Client) {
            s3Client = getNewClient();
        }
        return s3Client;
    }


    private AmazonS3 getNewClient() {

        System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(protocol);
        AmazonS3 s3Client = new AmazonS3Client(credentials, clientConfig);
        s3Client.setEndpoint(endpoint);
        return s3Client;
    }


    /**
     * @return
     * @description 创建Bucket 目前仅系统启动时执行
     * @author cuisihan
     * @date 2021-2-25 15:28
     */
    public void createBucket() {
        // 判断Bucket是否存在
        if (!getClient().doesBucketExist(bucketName)) {
            // 不存在则创建Bucket
            getClient().createBucket(new CreateBucketRequest(bucketName).withCannedAcl(CannedAccessControlList.PublicRead));
        }
    }

    /**
     * @return
     * @description 下载流
     * @author cuisihan
     * @date 2021-3-24 11:00
     */
    public InputStream downloadStream(String key) {
        S3Object object = getClient().getObject(new GetObjectRequest(bucketName, key));
        System.out.println("return s3 file InputStream");
        return object.getObjectContent();
    }

    /**
     * @return
     * @description 删除对象
     * @author cuisihan
     * @date 2021-3-24 16:31
     */
    public void deleteObject(String key) {
        getClient().deleteObject(new DeleteObjectRequest(bucketName, key));
    }


    /**
     * @param hdfsurl  hdfs url
     * @param key      key
     * @param hdfsfile hdfs文件地址
     * @return 成功返回true
     */
    public boolean uphdfs(String hdfsurl, String key,
                          String hdfsfile) {
        boolean fileflag = false;


        try {



            InputStream in = downloadStream(key);

            HDFSUtils hdfsUtils = new HDFSUtils();
            fileflag = hdfsUtils.ioupfile(hdfsurl, in, hdfsfile);

            System.out.println("s3 fileflag is"+fileflag);
            return fileflag;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fileflag;


    }

/*
    public static void main(String[] args) {
//
//        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
//        System.setProperty("javax.net.ssl.trustStore", "D:/jvm/DataCenterPlatform/jssecacerts");

//        System.out.println(">>>>>>>>>>>>" + System.getProperty("https.protocols"));
//        System.out.println(">>>>>>>>>>>>" + System.getProperty("javax.net.ssl.trustStore"));
        String endpoint = "10.150.41.201:9000";
//    // 公钥
        String accessKey = "kKBGW2W6vp09hPPZ";
//    // 密钥
//
        String secretKey = "y01gXuXKawacRYtkS75iVjX7m9c9IdTy";
//    // 桶
//
        String bucketName = "test";
//    // 协议类型

        String protocol = "https";

        S3Utils s3 = new S3Utils(endpoint, accessKey, secretKey, bucketName, protocol);
        boolean s3fl = s3.uphdfs("hdfs://192.168.22.9:8020", "8f9c65fb341140a0b3276646dbce6815/8f9c65fb341140a0b3276646dbce6815", "/par0/s3test/123.nc");
        System.out.println(s3fl);

    }*/
}
